Skip to content
This repository was archived by the owner on Jul 5, 2025. It is now read-only.

First request for Mongo FDW - #26

Open
DavidEdwards125 wants to merge 272 commits into
citusdata:masterfrom
EnterpriseDB:master
Open

First request for Mongo FDW#26
DavidEdwards125 wants to merge 272 commits into
citusdata:masterfrom
EnterpriseDB:master

Conversation

@DavidEdwards125

Copy link
Copy Markdown

First request for Mongo FDW

ibrarahmed1974 and others added 30 commits June 2, 2014 18:21
Conflicts:
	Makefile
	README.md
	mongo_fdw.c
	mongo_query.c
r Please enter the commit message for your changes. Lines starting
file to avoid conflict with other libraries.
Standard Support for both EDB Postgres Advanced Server 11 and
PostgreSQL 11 is already ended.  Thus, adjust Makefile so that we
restrict compilation of mongo_fdw code against v11.  Update the README
accordingly.  Also, clean up the code for the same.

FDW-673, Suraj Kharage, reviewed by Sravan Velagandula, tested by
Ajaykumar Pal.
Code changes involve adjusting calls to some server-side functions
due to their signature changes.

FDW-682, Suraj Kharage, reviewed by Jeevan Chalke.
This allows building the extension from the source's contrib directory
without the need for pg_config availability in the path.

Jeevan Chalke.
FDW-709, Vaibhav Dalvi, tested by Kashif Zeeshan.
The variable name we use to declare the column of outer relation
using the $let field is the same as the column name.  However, if
the column is a sub-column (i.e., "parent.child"), then the variable
name also contains the dot ("."), which is not allowed in variable
name formation, resulting in a wrong resultset being returned from
the server.

Since special characters other than underscore ("_") are not allowed,
let's use underscore instead of dot to fix the issue.

Reported on GitHub through issue #173 by Ömer Sezgin Uğurlu (ugurlu).

FDW-669, Vaibhav Dalvi, reviewed by Sravan Velagandula,
tested by Kashif Zeeshan.
Standard support has ended for EDB Postgres Advanced Server 12,
Postgres Extended Server 12, and PostgreSQL 12.  This commit removes
compatibility with v12 by updating the Makefile to prevent building
mongo_fdw against it.  Corresponding updates are made to the README,
and obsolete code paths related to v12 are cleaned up.

FDW-716, Vaibhav Dalvi, reviewed by Suraj Kharage, tested by
Ajaykumar Pal.
Pushable WHERE clause expressions were incorrectly added to the
MongoDB query pipeline using bsonAppend[Start/Finish]Array, leading
to the warning: "bson_append_array(): invalid array detected".  This
commit corrects the placement of the filter clause in the pipeline to
avoid the error and ensure proper query construction.

Reported on GitHub through issue #188 by Chris Williams (C-Williams).

DB-720, Vaibhav Dalvi, reviewed by Suraj Kharage, tested by
Ajaykumar Pal.
In join queries, the result is correctly labeled as "Join_Result".
However, while constructing the sort pipeline for the ORDER BY clause,
the column reference mistakenly used "Join_result" (with incorrect
casing).  This mismatch prevented the expected row ordering.  This
commit corrects the name usage to ensure proper ORDER BY pushdown.

DB-721, Vaibhav Dalvi, reviewed by Suraj Kharage, tested by
Ajaykumar Pal.
Ensure the buffer size is sufficient to safely hold a string
representation of an integer, including its sign.  Replace sprintf()
with snprintf() to avoid potential buffer overflows and improve
safety.

DB-722, Vaibhav Dalvi, reviewed by Suraj Kharage.
Upgrade the libmongoc driver to version 1.30.2 and the json-c library
to version 0.18-20240915.  Deprecated APIs have been replaced with
their modern equivalents as part of this update.

The previous versions had limitations that prevented reliable
connections to newer MongoDB servers, particularly version 8.

With this change, MongoDB 8 is now supported.  Compatibility with
MongoDB versions 5 and earlier may be affected, but those versions
are already end-of-life (EOL).

FDW-713, Vaibhav Dalvi, reviewed by Suraj Kharage, tested by
Kashif Zeeshan.
EDB Postgres Extended Server 18.

Code changes include updating calls to certain server-side functions
to match their modified signatures, along with adjustments to related
constants.

FDW-725, Vaibhav Dalvi, reviewed by Sravan Velagandula, tested by
Kashif Zeeshan.
Introduce a new GUC log_remote_query to enable logging of the remote
query that would be executed on the MongoDB server.  This is intended
for debugging purposes only.  When enabled, the remote query is
printed to the PostgreSQL server logs.  The default value is false.

FDW-729, Vaibhav Dalvi, reviewed and some adjustments by Jeevan Chalke.
I failed to add in 3333079.

Reported by Vaibhav Dalvi.
Replace sprintf() with snprintf() to prevent potential buffer
overflows and enhance code safety.  Also, remove the strlen() call
by using the length returned by bson_as_relaxed_extended_json().

Vaibhav Dalvi, reviewed by Jeevan Chalke.
…lauses.

Previously, join pushdown was restricted when full document retrieval
was involved in the target list, due to the lack of infrastructure for
correctly retrieving and processing full documents locally from join
results.  However, this restriction was not consistently applied to
restrict clauses (WHERE and JOIN clauses).  This oversight could lead
to incorrect results when such conditions were evaluated locally.

This commit extends the existing restriction to prevent join pushdown
when full document retrieval is involved in either the target list or
any restrict clause.

For similar reasons, this commit also restricts join pushdown when
whole row references are present in restrict clauses.  These changes
ensure correctness by preventing scenarios where local evaluation
cannot properly handle complex data types or complete row structures
derived from pushed-down joins.

Reported on GitHub through issue #191 by ephemeralctl.

FDW-733, Vaibhav Dalvi and Jeevan Chalke.
Standard support has ended for EDB Postgres Advanced Server 13,
Postgres Extended Server 13, and PostgreSQL 13.  This commit removes
compatibility with v13 by updating the Makefile to prevent building
mongo_fdw against it.  Corresponding updates are made to the README,
and obsolete code paths related to v13 are cleaned up.

FDW-744, Vaibhav Dalvi, reviewed by Suraj Kharage, tested by
Ajaykumar Pal.
Update the README to formally include EDB Postgres Extended (PGE)
Server in the list of supported database engines.  This clarifies
that mongo_fdw is fully compatible with PGE, in addition to standard
PostgreSQL and EDB Postgres Advanced Server (EPAS).

This README change helps users identify supported environments more
accurately when deploying the foreign data wrapper across different
EDB-supported Postgres distributions.

Vaibhav Dalvi
Update the "Running regression test" section to provide a more
structured setup guide.  This explicitly outlines the requirements
for environment variables and database/user configurations, and
highlights the need to execute mongodb_init.sh to seed test data
before running the test suite.

FDW-723, Vaibhav Dalvi, reported by Kashif Zeeshan
Add a section under Limitations explaining that column names with a
dollar sign ($) at the start or immediately following a dot (e.g.,
$field, nested.$field) are not supported.  While MongoDB allows
storing these field names, they cannot be referenced in aggregation
pipeline expressions, causing queries to return empty results.

Note that column names with $ in other positions (e.g., field$name)
function normally.

FDW-756, Vaibhav Dalvi
EDB Postgres Extended Server 19.

Version 19's c.h now relies on C11's static_assert(), so a C11
compiler is required.  The Makefile now selects -std=c11 for v19 and
later while continuing to use -std=c99 for earlier versions.  Also
add the missing <math.h> include required by rint().

Also, fetch the checkAsUser for a foreign modify's result relation
via ExecGetResultRelCheckAsUser() instead of reading ForeignScan's
checkAsUser field directly.

Added alternate expected output files to account for v19 changes in
EXPLAIN output (InitPlan/SubPlan naming, semi-join plan shape) and
other minor output differences.

FDW-771, Vaibhav Dalvi, reviewed by Jeevan Chalke.
Add a "WHERE clause push-down" section describing that WHERE clauses
on foreign tables are now pushed down to the remote MongoDB server,
reducing data transfer and improving query performance.  Update the
EXPLAIN example accordingly, since the pushed-down predicate no longer
shows up as a local Filter node.

Also replace the extension's own NUMERICARRAY_OID macro with
PostgreSQL's builtin NUMERICARRAYOID from pg_type, removing the now
redundant definition from mongo_query.h, and drop a stale, typo'd
comment in foreign_expr_walker() about same-table column comparisons
not being pushable.

FDW-749, Vaibhav Dalvi
Casting NaN or Infinity to an integer is undefined behavior in C, and
NaN comparisons always evaluate false, so range checks were silently
bypassed for these values.  Also, SMALLINT columns were cast down from
int32 without a range check, so a value like 2147483646 would silently
wrap around to -2.

Validate NaN/Infinity/range before casting, and add a dedicated
bsonIterInt16() so smallint conversions report "smallint" in the error
message rather than "integer", even when the source value also
overflows int32/int64.  The validation logic is shared via new
bson_double_to_int()/bson_int64_to_int() helpers to avoid duplicating
it per integer width.

FDW-757, Vaibhav Dalvi, reviewed and further improved by Jeevan
Chalke, tested by Kashif Zeeshan.
Converting a BSON double to int2/int4/int8 used a plain C cast, which
truncates toward zero.  PostgreSQL's own float-to-integer coercion
(dtoi2/dtoi4/dtoi8 in float.c) instead rounds to the nearest integer
first via rint(), so e.g. inserting 0.9 into a native int column
yields 1, while fetching the same value through mongo_fdw yielded 0.

Round before the range check in the shared bson_double_to_int()
helper, matching PostgreSQL's behavior exactly, including that
rounding can itself push a borderline value out of range (e.g. a
value just under INT32_MAX that rounds up past it).

Add regression coverage for 0.9/-0.9, a value that rounds into range,
one that rounds out of range, and round-half-to-even ties, across all
three integer widths.

FDW-769, Jeevan Chalke, per report from Kashif Zeeshan.
SUM(bigint)/AVG(bigint) etc. are computed as NUMERIC, which can hold
NaN and Infinity, so when such an aggregate is pushed down to MongoDB,
data that would raise "cannot convert NaN/infinity to bigint" on a
plain SELECT instead silently produces a NaN/Infinity result.  This is
not equivalent to summing a native numeric column that already
contains NaN: here the column is declared as an integer type, and the
per-row validation guarantee only holds for the row-fetch path, not
the pushed-down aggregate path.

Document this in the README's Limitations section, including the
enable_aggregate_pushdown workaround, and add regression coverage
showing both sides: the same SUM query silently returns NaN/Infinity
with aggregate pushdown enabled, and correctly errors once pushdown is
disabled and PostgreSQL computes it locally instead.

FDW-770, Jeevan Chalke, per report from Kashif Zeeshan.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.